# -*- encoding=utf8 -*-
__author__ = "chen"
from datetime import datetime
from common.common_util import *
def menpai(hwnd):
    connect_device(f"Windows:///{hwnd}")
    try:
        pcos = wait(Template(r"tpl1721049211855.png", record_pos=(-0.019, 0.179), resolution=(675, 530)),5)
        x = random.randint(-50, 50)
        y = random.randint(-2, 2)
        sleep(2)
        touch((pcos[0] + x, pcos[1] + y))
    except:
        print("xxxxxxxxxxxxx")
    try:
        pcos = wait(Template(r"tpl1721049218359.png", record_pos=(0.331, -0.09), resolution=(675, 530)))
        x = random.randint(-50, 50)
        y = random.randint(-2, 2)
        sleep(2)
        touch((pcos[0] + x, pcos[1] + y),2)

        pcos = wait(Template(r"tpl1721049226047.png", record_pos=(0.336, 0.145), resolution=(675, 530)))
        x = random.randint(-50, 50)
        y = random.randint(-2, 2)
        sleep(2)
        touch((pcos[0] + x, pcos[1] + y))
        return True
    except:
        print("xxxxxxxxxxxxx")
        return False


def get_hwnds(id):
    '''
    根据窗口标题获取对应句柄值
    :param id: 含有某个字段
    :return:
    '''
    hwnds = get_all_windows("梦幻西游")
    for i in range(len(hwnds)):
        title = win32gui.GetWindowText(hwnds[i])
        if f"[{id}]" in title:
            hwnd = hwnds[i]
            return hwnd
def quit_condit(json_data,condit):
    #判断结束程序条件,期望值与实际值相等，就结束执行
    ql = True
    for i in range(len(json_data)):
        fl = json_data[i][condit] == True
        ql = ql & fl
    return ql
def run_task(device_titles):
    json_data = []
    if len(device_titles)==0:
        hwnds = get_all_windows("梦幻西游")
        for i in range(len(hwnds)):
            data = {"dev":win32gui.GetWindowText(hwnds[i]),
                    "hwnd":hwnds[i],
                    "stop_falge": False,
                    "start_time":datetime.now()
                    }
            json_data.append(data)
    else:
        for i in range(len(device_titles)):
            data = {"dev": device_titles[i],
                    "hwnd": get_hwnds(device_titles[i]),
                    "stop_falge": False,
                    "start_time": datetime.now()
                    }
            json_data.append(data)


    for i in range(1000):
        print(json_data)
        for j in range(len(json_data)):
            if json_data[j]["stop_falge"]:
                continue
            else:
                reslut = menpai(json_data[j]["hwnd"])
                if reslut:
                    json_data[j]["start_time"] = datetime.now()
                else:
                    #  对是否有失败判断
                    try:
                        connect_device(f"Windows:///{json_data[j]['hwnd']}")
                        stop_falge = wait(Template(r"tpl1718504762862.png", record_pos=(0.002, -0.113), resolution=(494, 398)),2)
                        if stop_falge:
                            json_data[j]["stop_falge"] = True
                    except:
                        print("d")
                    finally:
                        #  对时间差值进行判断
                        if (datetime.now() - json_data[j]["start_time"]).seconds > 1000:
                            json_data[j]["stop_falge"] = True

        #结束判断
        if quit_condit(json_data, "stop_falge"):
            print(json_data)
            break

if __name__ == '__main__':
    #minimize_pycham()  # 最小化pycharm
    # 为空时，全部执行
    ex_hwns_ids = [3,10]
    print(ex_hwns_ids)
    run_task(ex_hwns_ids)